﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Display {
    import flash.events.*;
    import Engine.Managers.*;
    import flash.display.*;
    import Events.*;
    import Classes.*;
    import Transactions.*;
    import Classes.util.*;
    import Engine.*;

    public class TopFriendSelector extends SlotDialog {

        protected const NUM_SLOTS:int = 12;

        protected var m_selectedFriends:Array;
        protected var m_multiSelect:Boolean;
        protected var m_friendSelectorTitle:String = "FriendSelectorTitle";
        protected var m_errorPopup:GenericPopup;
        private var m_neighborNavigator:NeighborNavigator;
        private var m_friendsArray:Array;

        public function TopFriendSelector(_arg1:NeighborNavigator=null, _arg2:Boolean=false){
            var _local4:Boolean;
            var _local5:Number;
            this.m_selectedFriends = new Array();
            this.m_friendsArray = new Array();
            m_dialogAsset = "assets/dialogs/FV_FriendChooser.swf";
            this.m_multiSelect = _arg2;
            this.m_neighborNavigator = _arg1;
            var _local3:Number = 0;
            while (_local3 < Global.friendbar.length) {
                _local4 = false;
                _local5 = 0;
                while (_local5 < Global.player.topFriends.length) {
                    if (Global.friendbar[_local3].uid == Global.player.topFriends[_local5]){
                        _local4 = true;
                        break;
                    };
                    _local5++;
                };
                if (!_local4){
                    this.m_friendsArray.push(Global.friendbar[_local3]);
                };
                _local3++;
            };
            Global.world.citySim.neighborNavigationManager.clearFriendsPicturesData();
            super();
        }
        public function setTitle(_arg1:String):void{
            this.m_friendSelectorTitle = _arg1;
            if (m_window){
                m_window.friendChooser_mc.title_tf.text = ZLoc.t("Dialogs", this.m_friendSelectorTitle);
            };
        }
        public function getSelectedFriends():Array{
            return (this.m_selectedFriends);
        }
        override protected function onLoadComplete():void{
            m_window = (m_loader.content as MovieClip);
            m_window.friendChooser_mc.close_bt.addEventListener(MouseEvent.CLICK, this.onCloseClick);
            m_window.friendChooser_mc.arrowLt_bt.addEventListener(MouseEvent.CLICK, onLtArrowClick);
            m_window.friendChooser_mc.arrowRt_bt.addEventListener(MouseEvent.CLICK, onRtArrowClick);
            m_window.friendChooser_mc.title_tf.text = ZLoc.t("Dialogs", this.m_friendSelectorTitle);
            var _local1:GenericButton = new GenericButton(m_window.friendChooser_mc.next_bt, this.onNextClick);
            _local1.m_overlay.name = "next_bt";
            _local1.enableHitState(true);
            _local1.text = ZLoc.t("Dialogs", "NextCap");
            m_tiles.push(m_window.friendChooser_mc.friendCard1_mc, m_window.friendChooser_mc.friendCard2_mc, m_window.friendChooser_mc.friendCard3_mc, m_window.friendChooser_mc.friendCard4_mc, m_window.friendChooser_mc.friendCard5_mc, m_window.friendChooser_mc.friendCard6_mc, m_window.friendChooser_mc.friendCard7_mc, m_window.friendChooser_mc.friendCard8_mc, m_window.friendChooser_mc.friendCard9_mc, m_window.friendChooser_mc.friendCard10_mc, m_window.friendChooser_mc.friendCard11_mc, m_window.friendChooser_mc.friendCard12_mc);
            removeTiles();
            this.pushNewData();
            var _local2:int;
            while ((((_local2 < this.NUM_SLOTS)) && ((_local2 < m_data.length)))) {
                this.addComponent(_local2);
                _local2++;
            };
            addChild(m_window);
        }
        override protected function getNumSlots():int{
            return (this.NUM_SLOTS);
        }
        override protected function fixArrows():void{
            m_window.friendChooser_mc.arrowLt_bt.visible = ((m_tileSet)>0) ? true : false;
            m_window.friendChooser_mc.arrowRt_bt.visible = ((((m_tileSet + 1) * this.getNumSlots()))<m_data.length) ? true : false;
        }
        private function findFriendByIdFake(_arg1:String):Player{
            var _local6:Player;
            var _local2:Player;
            var _local3:Array = Global.friendbar;
            var _local4:Player = Global.player;
            var _local5:int;
            while (_local5 < Global.player.friends.length) {
                _local6 = (Global.player.friends[_local5] as Player);
                if (Global.player.neighbors[_local5] == _arg1){
                    _local2 = _local6;
                    break;
                };
                _local5++;
            };
            return (_local2);
        }
        protected function pushNewData():void{
            var _local1:int;
            var _local2:Player;
            m_data = [];
            m_tileSet = 0;
            if (this.m_friendsArray){
                _local1 = 0;
                while (_local1 < this.m_friendsArray.length) {
                    _local2 = Global.player.findFriendById(this.m_friendsArray[_local1].uid);
                    if (_local2){
                        m_data.push(_local2);
                    };
                    _local1++;
                };
            };
            this.fixArrows();
        }
        override protected function addComponent(_arg1:int, _arg2:int=0):void{
            var tile:* = null;
            var url:* = null;
            var icon:* = null;
            var dispObj:* = null;
            var compId:* = _arg1;
            var index:int = _arg2;
            if (((m_tiles) && (!((m_data[(index + compId)] == null))))){
                tile = (m_tiles[compId] as MovieClip);
                tile.visible = true;
                tile.addEventListener(MouseEvent.MOUSE_OVER, this.onTileMouseOver);
                tile.addEventListener(MouseEvent.MOUSE_OUT, this.onTileMouseOut);
                tile.addEventListener(MouseEvent.CLICK, this.onTileMouseClick);
                tile.id = (index + compId);
                tile.itemTitle_tf.text = m_data[(index + compId)].name;
                url = m_data[(index + compId)].snUser.picture;
                if (url){
                    icon = LoadingManager.load(url, function (_arg1:Event):void{
                        var _local2:*;
                        if (((icon) && (icon.content))){
                            if (tile){
                                Utilities.removeAllChildren(tile.icon);
                            };
                            _local2 = icon.content;
                            _local2.width = 50;
                            _local2.height = 50;
                            tile.icon.addChild(_local2);
                            Global.world.citySim.neighborNavigationManager.addFriendPictureData(_local2, m_data[(index + compId)].uid);
                        };
                    });
                } else {
                    if (tile){
                        Utilities.removeAllChildren(tile.icon);
                    };
                    dispObj = new EmbeddedArt.hud_no_profile_pic();
                    dispObj.width = 50;
                    dispObj.height = 50;
                    tile.icon.addChild(dispObj);
                    Global.world.citySim.neighborNavigationManager.addFriendPictureData(dispObj, m_data[(index + compId)].uid);
                };
                tile.checked = false;
                if (this.isFriendSelected((m_data[(index + compId)] as Player))){
                    tile.checked = true;
                };
                if (tile.checked){
                    tile.gotoAndStop(3);
                } else {
                    tile.gotoAndStop(1);
                };
            };
        }
        protected function onTileMouseOver(_arg1:MouseEvent):void{
            var _local2:MovieClip;
            if (_arg1.target.hasOwnProperty("checked")){
                _local2 = (_arg1.target as MovieClip);
            } else {
                _local2 = (_arg1.target.parent as MovieClip);
            };
            if (_local2.checked){
                _local2.gotoAndStop(4);
            } else {
                _local2.gotoAndStop(2);
            };
        }
        protected function onTileMouseOut(_arg1:MouseEvent):void{
            var _local2:MovieClip;
            if (_arg1.target.hasOwnProperty("checked")){
                _local2 = (_arg1.target as MovieClip);
            } else {
                _local2 = (_arg1.target.parent as MovieClip);
            };
            if (_local2.checked){
                _local2.gotoAndStop(3);
            } else {
                _local2.gotoAndStop(1);
            };
        }
        protected function onTileMouseClick(_arg1:MouseEvent):void{
            var _local2:MovieClip;
            if (_arg1.target.hasOwnProperty("checked")){
                _local2 = (_arg1.target as MovieClip);
            } else {
                _local2 = (_arg1.target.parent as MovieClip);
            };
            _local2.checked = !(_local2.checked);
            if (_local2.checked){
                this.handleAddFriend(_local2.id);
                _local2.gotoAndStop(4);
            } else {
                this.handleRemoveFriend(_local2.id);
                _local2.gotoAndStop(2);
            };
        }
        protected function isFriendSelected(_arg1:Player):Boolean{
            var _local3:int;
            var _local2:Boolean;
            if (_arg1){
                _local3 = 0;
                while (_local3 < this.m_selectedFriends.length) {
                    if (((this.m_selectedFriends[_local3]) && ((this.m_selectedFriends[_local3].getId() == _arg1.getId())))){
                        _local2 = true;
                        break;
                    };
                    _local3++;
                };
            };
            return (_local2);
        }
        protected function handleAddFriend(_arg1:int):void{
            var _local2:int;
            var _local3:MovieClip;
            if (((!(this.m_multiSelect)) && ((this.m_selectedFriends.length > 0)))){
                _local2 = m_data.indexOf(this.m_selectedFriends[0]);
                if ((((_local2 >= (m_tileSet * this.getNumSlots()))) && ((_local2 < ((m_tileSet * this.getNumSlots()) + this.getNumSlots()))))){
                    _local3 = (m_tiles[(_local2 - (m_tileSet * this.getNumSlots()))] as MovieClip);
                    _local3.checked = false;
                    _local3.gotoAndStop(1);
                };
                this.m_selectedFriends = new Array();
            };
            this.m_selectedFriends.push(m_data[_arg1]);
        }
        protected function handleRemoveFriend(_arg1:int):void{
            var _local2:int = this.m_selectedFriends.indexOf(m_data[_arg1]);
            this.m_selectedFriends.splice(_local2, 1);
        }
        protected function errorPopupClose(_arg1:Event):void{
            this.m_errorPopup = null;
        }
        protected function onNextClick(_arg1:MouseEvent):void{
            var _local2:Number;
            var _local3:Number;
            if (this.m_selectedFriends.length > 0){
                if (this.m_neighborNavigator){
                    if (this.m_neighborNavigator.containsNeighbor){
                        Global.player.topFriends[Global.player.topFriends.indexOf(this.m_neighborNavigator.currentTopNeighbor.uid)] = "-1";
                        GameTransactionManager.addTransaction(new TUpdateTopFriends(Global.player.topFriends));
                        this.m_neighborNavigator.getDisplayObject().parent.removeChild(this.m_neighborNavigator.neighborPic);
                        this.m_neighborNavigator.neighborPic = null;
                    };
                    _local2 = Global.world.citySim.neighborNavigationManager.neighborNavigators.indexOf(this.m_neighborNavigator);
                    Global.player.topFriends[_local2] = this.m_selectedFriends[0].uid;
                    GameTransactionManager.addTransaction(new TUpdateTopFriends(Global.player.topFriends));
                    this.m_neighborNavigator.currentTopNeighbor = this.m_selectedFriends[0];
                    this.m_neighborNavigator.containsNeighbor = true;
                    this.m_neighborNavigator.setItem(Global.gameSettings.getItemByName("prop_filled_top_neighbor"));
                    this.m_neighborNavigator.reloadImage();
                    _local3 = Global.world.citySim.neighborNavigationManager.findSelectedFriendPictureIndex(this.m_selectedFriends[0]);
                    this.m_neighborNavigator.neighborPic = Global.world.citySim.neighborNavigationManager.friendsPictures[_local3];
                    this.m_neighborNavigator.displayFriendPicOnBillboard();
                };
                if (this.m_errorPopup){
                    this.m_errorPopup.close();
                };
                removeTiles(false);
                m_window.friendChooser_mc.close_bt.removeEventListener(MouseEvent.CLICK, this.onCloseClick);
                m_window.friendChooser_mc.arrowLt_bt.removeEventListener(MouseEvent.CLICK, onLtArrowClick);
                m_window.friendChooser_mc.arrowRt_bt.removeEventListener(MouseEvent.CLICK, onRtArrowClick);
                _arg1.stopPropagation();
                close();
                dispatchEvent(new CloseEvent(CloseEvent.CLOSE));
            } else {
                if (this.m_errorPopup == null){
                    if (this.m_neighborNavigator){
                        if (this.m_neighborNavigator.containsNeighbor){
                            Global.player.topFriends[Global.player.topFriends.indexOf(this.m_neighborNavigator.currentTopNeighbor.uid)] = "-1";
                            GameTransactionManager.addTransaction(new TUpdateTopFriends(Global.player.topFriends));
                            this.m_neighborNavigator.getDisplayObject().parent.removeChild(this.m_neighborNavigator.neighborPic);
                            this.m_neighborNavigator.neighborPic = null;
                        };
                        this.m_neighborNavigator.currentTopNeighbor = null;
                        this.m_neighborNavigator.containsNeighbor = false;
                        this.m_neighborNavigator.setItem(Global.gameSettings.getItemByName("prop_empty_top_neighbor"));
                        this.m_neighborNavigator.reloadImage();
                    };
                    if (this.m_errorPopup){
                        this.m_errorPopup.close();
                    };
                    this.m_selectedFriends = new Array();
                    removeTiles(false);
                    m_window.friendChooser_mc.close_bt.removeEventListener(MouseEvent.CLICK, this.onCloseClick);
                    m_window.friendChooser_mc.arrowLt_bt.removeEventListener(MouseEvent.CLICK, onLtArrowClick);
                    m_window.friendChooser_mc.arrowRt_bt.removeEventListener(MouseEvent.CLICK, onRtArrowClick);
                    _arg1.stopPropagation();
                    close();
                    dispatchEvent(new CloseEvent(CloseEvent.CLOSE));
                };
            };
        }
        protected function onCloseClick(_arg1:MouseEvent):void{
            if (this.m_errorPopup){
                this.m_errorPopup.close();
            };
            this.m_selectedFriends = new Array();
            removeTiles(false);
            m_window.friendChooser_mc.close_bt.removeEventListener(MouseEvent.CLICK, this.onCloseClick);
            m_window.friendChooser_mc.arrowLt_bt.removeEventListener(MouseEvent.CLICK, onLtArrowClick);
            m_window.friendChooser_mc.arrowRt_bt.removeEventListener(MouseEvent.CLICK, onRtArrowClick);
            _arg1.stopPropagation();
            close();
            dispatchEvent(new CloseEvent(CloseEvent.CLOSE));
        }

    }
}//package Display 
